package com.npjscg.medicalpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.common.Constants;
import com.npjscg.medicalpro.controller.dto.TemplateDTO;
import com.npjscg.medicalpro.entity.Template;
import com.npjscg.medicalpro.entity.User;
import com.npjscg.medicalpro.exception.ServiceException;
import com.npjscg.medicalpro.mapper.TemplateMapper;
import com.npjscg.medicalpro.service.ITemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.npjscg.medicalpro.service.IUserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author raote
 * @since 2022-06-28
 */
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements ITemplateService {

    @Autowired
    private IUserService userService;

    @Resource
    private TemplateMapper templateMapper;

    @Override
    public IPage selectPage(Page<Template> page, int adminid, String temname) {

        if (userService.getById(adminid) == null || userService.getById(adminid).getRole() == 0)
            throw new ServiceException(Constants.CODE_600, "数据库中没有此用户 或者 该用户未拥有管理员权限");
        else {
            QueryWrapper<Template> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("adminid", adminid);
            queryWrapper.like("tem_name", temname);
            return templateMapper.selectPage(page,queryWrapper);
        }

    }

    @Override
    public Boolean newTemplate(Template template) {
        if (userService.getById(template.getAdminid()) == null || userService.getById(template.getAdminid()).getRole() == 0)
            throw new ServiceException(Constants.CODE_600, "数据库中没有此用户 或者 该用户未拥有管理员权限");
        else
            return save(template);
    }

    @Override
    public void delete(int id) {
        Template byId = getById(id);
        if (byId == null)
            throw new ServiceException(Constants.CODE_600, "数据库中不存在该记录无法删除");
        else
            templateMapper.deleteById(id);
    }

    @Override
    public IPage selectAll(Page<Template> page, int uid, String temname) {
        QueryWrapper<Template> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("tem_name", temname);
        return templateMapper.selectUnCompletePage(page,temname,uid);
    }

    @Override
    public Map<String, Integer> getRateList(int id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role" , 0);
        int count = userService.count(queryWrapper);

        Map<String , Integer> rateMap = new HashMap<>();
        Integer data0 = templateMapper.getData(0, id);
        Integer data1 = templateMapper.getData(1, id);
        Integer data2 = templateMapper.getData(2, id);
        Integer rest = count-data0-data1-data2;

        rateMap.put("unfill", rest);
        rateMap.put("uncheck", data0);
        rateMap.put("unpass", data1);
        rateMap.put("pass", data2);

        return rateMap;
    }


}
